﻿using System;
using System.Collections.Generic;
using System.Text;
using EnterpriseDT.Net.Ftp;
//缺少编译器要求的成员“ystem.Runtime.CompilerServices.ExtensionAttribute..ctor”
namespace System.Runtime.CompilerServices
{
    public class ExtensionAttribute : Attribute { }
}
namespace DSAManager.Utils
{

    public static class FtpClientExtensions
    {
        /// <summary>
        /// 递归获取目录的所有子目录和文件信息
        /// </summary>
        public static FTPFile[] GetFileInfosRecursive(this FTPConnection conn)
        {
            var resultList = new List<FTPFile>();
            var fileInfos = conn.GetFileInfos();
            resultList.AddRange(fileInfos);
            foreach (var fileInfo in fileInfos)
            {
                if (fileInfo.Dir)
                {
                    conn.ServerDirectory = fileInfo.Path;
                    resultList.AddRange(conn.GetFileInfosRecursive());
                }
            }
            return resultList.ToArray();
        }
        /// <summary>
        /// 递归删除目录(包括所有子目录和文件信息)
        /// </summary>
        public static void DeleteDirectoryRecursive(this FTPConnection conn, string directoryName)
        {
            conn.ChangeWorkingDirectory(directoryName);
            var fileInfos = conn.GetFileInfos();
            foreach (var fileInfo in fileInfos)
            {
                if (fileInfo.Name == "." || fileInfo.Name == "..") continue;
                
                if (fileInfo.Dir)
                    conn.DeleteDirectoryRecursive(fileInfo.Name);
                else
                    conn.DeleteFile(fileInfo.Name);
            }
            conn.ChangeWorkingDirectoryUp();
            conn.DeleteDirectory(directoryName);
        }
    }
}
